package com.sit.library.controller;

import com.sit.library.entity.bean.Book;
import com.sit.library.entity.bean.BorrowRecord;
import com.sit.library.entity.bean.RestBean;
import com.sit.library.entity.vo.response.BorrowRecordVO;
import com.sit.library.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/find/book")
    public String findBook() {
        List<Book> books = userService.getBooks();
        return RestBean.success(books).asJsonString();
    }

    @PostMapping("/find/book")
    public String findBookByTitle(@RequestParam String title) {
        List<Book> books = userService.getBooksByTitle(title);
        return RestBean.success(books).asJsonString();
    }

    @GetMapping("/find/record")
    public String findRecord(@RequestHeader("Authorization") String token) {
        List<BorrowRecordVO> recordsVO = userService.getBorrowRecords(token);
        return RestBean.success(recordsVO).asJsonString();
    }

    @PostMapping("/borrow/{bookId}")
    public String borrowBook(@PathVariable int bookId, @RequestHeader("Authorization") String token) {
        String result = userService.borrowBook(token, bookId);
        return RestBean.success(result).asJsonString();
    }

    @PostMapping("/return/{recordId}")
    public String returnBook(@PathVariable int recordId, @RequestHeader("Authorization") String token) {
        String result = userService.returnBook(token, recordId);
        return RestBean.success(result).asJsonString();
    }
}
